package com.smiier.skin;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.o.app.json.JsonUtil;
import cn.o.app.ui.OImageView;
import cn.o.app.util.OUtil;

import com.evan.common.utils.CommonUtility;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.entity.RelationShip;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.net.entity.UserWrapper;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;

public class PatientOfDoctorInfo extends BasicActivity {
	private OImageView mLogoImage;
	private TextView mPatientName;
	private TextView mProfile;
	private TextView mVisit;
	private TextView condition;

	private TextView source_from_txt;
	private TextView phone_txt;
	private TextView visit_time_txt;
	private TextView diagnose_txt;

	private RelativeLayout source_from;
	private RelativeLayout phone;
	private RelativeLayout visit_time;
	private RelativeLayout diagnose; // 诊断

	protected Drawable mDrawableOffline;
	protected Drawable mDrawableOnline;
	protected Drawable mDrawableProject;

	JSONObject mData;
	UserWrapper mUserData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getData();

	}

	private void initView() {
		mLogoImage = findViewById(R.id.logo, OImageView.class);
		mPatientName = findViewById(R.id.name, TextView.class);
		mProfile = findViewById(R.id.profile, TextView.class);
		mVisit = findViewById(R.id.visit, TextView.class);

		source_from = findViewById(R.id.source_from, RelativeLayout.class);
		source_from_txt = findViewById(R.id.source_from_txt, TextView.class);
		phone = findViewById(R.id.phone, RelativeLayout.class);
		phone_txt = findViewById(R.id.phone_txt, TextView.class);
		visit_time = findViewById(R.id.visit_time, RelativeLayout.class);
		visit_time_txt = findViewById(R.id.visit_time_txt, TextView.class);
		diagnose = findViewById(R.id.diagnose, RelativeLayout.class);
		diagnose_txt = findViewById(R.id.diagnose_txt, TextView.class);
		condition = findViewById(R.id.condition, TextView.class);

		Resources res = this.getContext().getResources();
		mDrawableOffline = res.getDrawable(R.drawable.source_from_offline);
		mDrawableOnline = res.getDrawable(R.drawable.source_from_online);
		mDrawableProject = res.getDrawable(R.drawable.source_from_project);
	}

	private void getData() {
		try {
			mData = new JSONObject(getIntent().getStringExtra(
					Constant.IDENTITY_KEY));

			mUserData = JsonUtil.convertFromObject(mData, UserWrapper.class);

			Log.i("testisempty",
					"Diagnose" + (mUserData.RelationShip.Diagnose.toString()));
			Log.i("testisempty", "Diagnose.boolean"
					+ (mUserData.RelationShip.Diagnose.isEmpty()));

			if (!mUserData.RelationShip.Diagnose.isEmpty()) {

				setContentView(R.layout.view_patient_of_doctor_detial_head);

				init();

				initDifferentHeadTitle();

				initView();
				initViewByDaa();
			} else {
				/**
				 * 当没有诊断结果的时候--- 走诊断流程
				 */
				Intent intent = new Intent();
				intent.setClass(PatientOfDoctorInfo.this,
						PatientConditionInfo.class);
				startActivity(intent);
				this.finish();
			}

			Log.i("testquestioninfo", "mData:" + (mData));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void initViewByDaa() {
		if (mData != null && !CommonUtility.isNull(mData)) {
			String logoUrl = GlobalSettings.SERVER_IMG_URL + mUserData.User.Pic;
			if (logoUrl.lastIndexOf(".") == -1) {
				logoUrl += GlobalSettings.IMG_THUMB;
			}

			mBitmapUtils.display(mLogoImage, logoUrl);
			mPatientName.setText(mUserData.User.Name);

			StringBuilder sb = new StringBuilder();
			Date today = new Date();
			if (mUserData.User.Sex == User.SEX_MALE) {
				sb.append("男,");
			} else if (mUserData.User.Sex == User.SEX_FEMALE) {
				sb.append("女,");
			}
			sb.append(today.getYear() - mUserData.User.Birth.getYear());
			sb.append("岁");
			if (!OUtil.isEmpty(mUserData.RelationShip.Diagnose)) {
				sb.append(",");
				sb.append(mUserData.RelationShip.Diagnose);
			}

			mProfile.setText(sb.toString());

			if (mUserData.RelationShip.RSSource == RelationShip.SOURCE_OFFLINE) {
				source_from_txt.setText("线下导入");
				source_from_txt.setCompoundDrawablesWithIntrinsicBounds(null,
						null, mDrawableOffline, null);
			} else if (mUserData.RelationShip.RSSource == RelationShip.SOURCE_ONLINE) {
				source_from_txt.setVisibility(View.VISIBLE);
				source_from_txt.setText("在线咨询");
				source_from_txt.setCompoundDrawablesWithIntrinsicBounds(null,
						null, mDrawableOnline, null);
			} else {

			}

			phone_txt.setText(mUserData.User.Mobile);
			visit_time_txt.setText(mUserData.RelationShip.Visiting_Time
					.toString());
			diagnose_txt.setText(mUserData.RelationShip.Diagnose);
			condition.setText(mUserData.RelationShip.Condition);

			// mProfile.setText(mUserData.User.Sex+","+mUserData.User.);

		} else {
			// 显示数据出错
		}

	}

	private void initDifferentHeadTitle() {
		text_nav.setText("用户名称");
	}

}
